// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-25

package nacos

import (
	"fmt"
	"gitee.com/go-libs/console"
	"kv/providers"
	"regexp"
)

const (
	defaultGroup = "DEFAULT_GROUP"
	defaultHost  = "127.0.0.1:8848"

	optionDataId    = "data-id"
	optionHost      = "host"
	optionGroup     = "group"
	optionNamespace = "namespace"
	optionOverride  = "override"
	optionPath      = "path"

	providerDesc = "Read configuration from nacos server then save as local files"
	providerName = "nacos"
)

var (
	errDataIdNotSpecified    = fmt.Errorf(`data id not specified`)
	errPathNotSpecified      = fmt.Errorf(`location path not specified`)
	errPathNotValidDirectory = fmt.Errorf(`location path not a valid directory`)

	options = map[string]providers.Option{
		optionDataId: {
			Description: "Data ID, eg: gmq",
			Kind:        console.KindString,
			Required:    true,
			Short:       'd',
		},
		optionGroup: {
			DefaultValue: defaultGroup,
			Description:  "Nacos group name",
			Kind:         console.KindString,
			Short:        'g',
		},
		optionHost: {
			DefaultValue: defaultHost,
			Description:  "Nacos server address",
			Kind:         console.KindString,
			Short:        'h',
		},
		optionNamespace: {
			Description: "Nacos namespace",
			Kind:        console.KindString,
			Short:       'n',
		},
		optionPath: {
			Description: "Location for Generated files, eg: /data/config",
			Kind:        console.KindString,
			Required:    true,
			Short:       'p',
		},
		optionOverride: {
			Description: "Override the generated file if exists",
			Kind:        console.KindNull,
			Short:       'o',
		},
	}

	regexEndWithSlash           = regexp.MustCompile(`/+$`)
	regexMatchFilename          = regexp.MustCompile(`^(.+)/([^/]+)$`)
	regexStartWithProtocol      = regexp.MustCompile(`^(?i)https?://`)
	regexWithCompatibleVariable = regexp.MustCompile(`(?i)kv://([,./_a-z0-9-]+)`)
	regexWithVariable           = regexp.MustCompile(`{{([^}]+)}}`)
)
